package com.ethen.jzvd.widget;

import com.ethen.jzvd.ResourceTable;
import com.ethen.jzvd.ResourceTool;
import ohos.agp.components.*;
import ohos.agp.text.Font;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.service.WindowManager;
import ohos.app.Context;

import java.util.Optional;

public class AlertDialog implements DialogInterface {
    private static final int MAX_BUTTON_NUMBER = 3;

    private static final int DIALOG_WIDTH_VP = 328;

    private final String[] mButtonTexts = new String[MAX_BUTTON_NUMBER];

    private final int[] mButtonTypes = new int[MAX_BUTTON_NUMBER];

    private final OnClickListener[] mButtonListeners = new OnClickListener[MAX_BUTTON_NUMBER];

    private String mTitle;

    private String mContent;

    private int mIconId;

    private int buttonNum = 0;

    private float dim = -1f;

    private boolean mOutsideTouchClosable = false;

    private CommonDialog mCommonDialog;

    public void show() {
        if (mCommonDialog != null) {
            mCommonDialog.show();
            if (dim >= 0) {
                changeDialogDim(mCommonDialog, dim);
            }
        }
    }

    public void remove(){
        if (mCommonDialog != null) {
            mCommonDialog.destroy();
        }
    }

    private void changeDialogDim(CommonDialog dialog, float dim) {
        Optional<WindowManager.LayoutConfig> configOpt = dialog.getWindow().getLayoutConfig();
        configOpt.ifPresent(config -> {
            config.dim = dim;
            dialog.getWindow().setLayoutConfig(config);
        });
    }

    @Override
    public void cancel() {

    }

    @Override
    public void dismiss() {

    }


    public static class Builder {
        private final AlertDialog mGeneralDialog;

        private final Context mContext;

        public Builder(Context context) {
            mContext = context;
            mGeneralDialog = new AlertDialog();
        }

        public Builder setTitle(String title) {
            mGeneralDialog.mTitle = title;
            return this;
        }

        public Builder setMessage(String content) {
            mGeneralDialog.mContent = content;
            return this;
        }

        public Builder setIcon(int id) {
            mGeneralDialog.mIconId = id;
            return this;
        }

        public Builder setDim(float dim){
            if (dim > 1) {
                mGeneralDialog.dim = 1;
            } else if (dim < 0) {
                mGeneralDialog.dim = 0;
            } else {
                mGeneralDialog.dim = dim;
            }
            return this;
        }

        public Builder setOutsideTouchClosable(boolean closable){
            mGeneralDialog.mOutsideTouchClosable = closable;
            return this;
        }

        public Builder setPositiveButton(String content,OnClickListener listener) {
            addButton(content,BUTTON_POSITIVE,listener);
            return this;
        }

        public Builder setNegativeButton(String content,OnClickListener listener) {
            addButton(content,BUTTON_NEGATIVE,listener);
            return this;
        }

        public Builder setOnCancelListener(String content,OnClickListener listener) {
            addButton(content,BUTTON_NEGATIVE,listener);
            return this;
        }

        public Builder setOnCancelListener(OnClickListener listener) {
            return setOnCancelListener(ResourceTool.getString(mContext,ResourceTable.String_alert_cancel,null),listener);
        }

        private Builder addButton(String text, int type, OnClickListener listener) {
            if (mGeneralDialog.buttonNum >= MAX_BUTTON_NUMBER) {
                return this;
            }
            mGeneralDialog.mButtonTexts[mGeneralDialog.buttonNum] = text;
            mGeneralDialog.mButtonTypes[mGeneralDialog.buttonNum] = type;
            mGeneralDialog.mButtonListeners[mGeneralDialog.buttonNum] = listener;
            mGeneralDialog.buttonNum++;
            return this;
        }

        public AlertDialog create() {
            CommonDialog sDialog = new CommonDialog(mContext);
            sDialog.setSize(AttrHelper.fp2px(DIALOG_WIDTH_VP, mContext), ComponentContainer.LayoutConfig.MATCH_CONTENT);
            sDialog.setAlignment(LayoutAlignment.BOTTOM);
            sDialog.setOffset(0, (int) ResourceTool.getFloat(mContext, ResourceTable.Float_spacing_huge, 0));
            sDialog.setTransparent(true);
            sDialog.setContentCustomComponent(initDialog());
            sDialog.setAutoClosable(mGeneralDialog.mOutsideTouchClosable);
            mGeneralDialog.mCommonDialog = sDialog;
            return mGeneralDialog;
        }

        private Component initDialog() {
            Component dialogLayout = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_jz_dialog_alert, null, false);

            Text textTitle = (Text) dialogLayout.findComponentById(ResourceTable.Id_title);
            if (mGeneralDialog.mTitle != null) {
                textTitle.setText(mGeneralDialog.mTitle);
                textTitle.setVisibility(Component.VISIBLE);
            }

            Image imageIcon = (Image) dialogLayout.findComponentById(ResourceTable.Id_icon);
            if (mGeneralDialog.mIconId != 0) {
                imageIcon.setPixelMap(mGeneralDialog.mIconId);
                imageIcon.setVisibility(Component.VISIBLE);
                Component titleLayout = dialogLayout.findComponentById(ResourceTable.Id_title_layout);
                titleLayout.setVisibility(Component.HIDE);
            }

            Text textContent = (Text) dialogLayout.findComponentById(ResourceTable.Id_content);
            Component bottomLayout = dialogLayout.findComponentById(ResourceTable.Id_bottom_layout);
            if (mGeneralDialog.mContent != null) {
                textContent.setText(mGeneralDialog.mContent);
                textContent.setVisibility(Component.VISIBLE);
                bottomLayout.setVisibility(Component.VISIBLE);
            }

            DirectionalLayout operationLayout = (DirectionalLayout) dialogLayout.findComponentById(ResourceTable.Id_operation_layout);
            float totalWeight = 1.25f;
            for (int i = 0; i < mGeneralDialog.buttonNum; i++) {
                if (i == 0) {
                    operationLayout.setVisibility(Component.VISIBLE);
                    bottomLayout.setVisibility(Component.VISIBLE);
                }
                Button button = new Button(mContext);
                operationLayout.addComponent(button);
                DirectionalLayout.LayoutConfig config = new DirectionalLayout.LayoutConfig();
                config.height = (int) ResourceTool.getFloat(mContext, ResourceTable.Float_button_height, 0);
                config.width = DirectionalLayout.LayoutConfig.MATCH_PARENT;
                config.weight = 1;
                int spacingHorizontal = (int) ResourceTool.getFloat(mContext, ResourceTable.Float_spacing_mini, 0);
                config.setMargins(spacingHorizontal, 0, spacingHorizontal, 0);
                button.setLayoutConfig(config);
                if (i != 0) {
                    totalWeight = i + 1.0f;
                }

                button.setText(mGeneralDialog.mButtonTexts[i]);
                int currentIndex = i;
                button.setClickedListener(component -> mGeneralDialog.mButtonListeners[currentIndex].onClick(mGeneralDialog,mGeneralDialog.mButtonTypes[currentIndex]));
                button.setFont(Font.DEFAULT_BOLD);
                button.setTextSize((int) ResourceTool.getFloat(mContext, ResourceTable.Float_button_size, 0));
                button.setMultipleLine(false);
                button.setTruncationMode(Text.TruncationMode.ELLIPSIS_AT_END);
                button.setPaddingLeft(spacingHorizontal);
                button.setPaddingRight(spacingHorizontal);
            }
            operationLayout.setTotalWeight(totalWeight);
            return dialogLayout;
        }
    }
}
